package org.dizem.groupon.action;

import org.dizem.groupon.model.domain.UserEntity;
import org.dizem.groupon.service.UserService;
import org.dizem.util.Pager;

import java.util.List;

/**
 * User: dizem@126.com
 * Time: 11-6-27 上午11:06
 * <p/>
 * 取得所有用户列表
 */
public class UserAction extends BaseAction {

	private List<UserEntity> users;
	private UserService service;
	private int id;
	private String accountInfo;
	private String cityName;
	private UserEntity user;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getAccountInfo() {
		return accountInfo;
	}

	public void setAccountInfo(String accountInfo) {
		this.accountInfo = accountInfo;
	}

	public String getCityName() {
		return cityName;
	}

	public void setCityName(String cityName) {
		this.cityName = cityName;
	}

	public UserEntity getUser() {
		return user;
	}

	public void setUser(UserEntity user) {
		this.user = user;
	}

	public List<UserEntity> getUsers() {
		return users;
	}

	public void setUsers(List<UserEntity> users) {
		this.users = users;
	}

	public UserService getService() {
		return service;
	}

	public void setService(UserService service) {
		this.service = service;
	}

	public String list() {
		if (this.getRecordCount() < 0) {
			setRecordCount(service.getCount());
		}
		if (getPage() == null) {
			setPage(1);
		}
		Pager p = new Pager(getPage(), getRecordCount(), getPageSize());
		setPageCount(p.getPageCount());
		setPageUrl("listUser");
		setPage(p.getPage());
		users = service.load(p.getStartPage(), this.getPageSize());
		return SUCCESS;
	}

	public String delete() throws Exception {
		UserEntity user = service.getById(id);
		if (user != null)
			service.delete(user);
		return SUCCESS;
	}

	public String update() throws Exception {
		System.out.println(accountInfo);
		if (accountInfo.equals("管理员")) {
			user.setAccountType(1);
		} else {
			user.setAccountType(0);
		}
		service.update(user, cityName);

		if (user.getAccountType() == 0)
			return SUCCESS;
		else
			return "adminSuccess";
	}

	public String edit() throws Exception {
		try {
			user = service.getById(id);
			if (user.getAccountType() == 1) {
				accountInfo = "管理员";
			} else {
				accountInfo = "普通用户";
			}
			cityName = user.getCity().getChnName();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return super.execute();
	}
}
